library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(googlesheets4)
library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(lubridate) # this package is part of tidyverse but not loaded by default
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(repurrrsive)
library(tools)

library(tmap)
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(geojsonsf)
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.6.2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(mapview)

library(httr)
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
library(dplyr)
library(stringr)

library(forcats)
library(infer)
## Warning: package 'infer' was built under R version 3.6.2

Dormitory locations data

Loading dormitory locations data

dormitories <- read_csv("data/dormitories_cleaned.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_double(),
##   dormitory = col_character(),
##   address_of_dormitory = col_character(),
##   lon = col_double(),
##   lat = col_double()
## )

Clean dormitory dataset

dormitories <- dormitories %>%
  filter (dormitory != "NA")
dormitories <- dormitories %>%
  clean_names()
dormitories <- dormitories %>% 
mutate(dormitory = recode(dormitory,`Kian Teck Dormitory\n` = "Kian Teck Dormitory",`Kian Teck Hostel\n` = "Kian Teck Hostel",`Blue Stars Dormitory\n` = "Blue Stars Dormitory", `Jurong Apartments\n` = "Jurong Apartments", `Avery Lodge\n` = "Avery Lodge", `Cassia @ Penjuru\n` = "Cassia @ Penjuru")) %>%
  mutate (address_of_dormitory = recode (address_of_dormitory, `16 Tuas Avenue 1 #07-201 Singapore 639529` = "14 Tuas Ave 1, Singapore 639499"))

Geocoding

register_google(key = "AIzaSyD5QiSXsdezyH3ylxzOH43WOnQJOwgZxTM")

dormitories$lon <- NA
dormitories$lat <- NA
# Loop through the addresses to get the latitude and longitude of each address and add it to the
# dormitories data frame in new columns lat and lon
for(i in 1:nrow(dormitories)) {
  result <- geocode(dormitories$address_of_dormitory[i], output = "latlona", source = "google")
  dormitories$lon[i] <- as.numeric(result[1])
  dormitories$lat[i] <- as.numeric(result[2])
}
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=540+Bukit+Batok+Street+23,+Singapore+659553&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=19A+Toh+Guan+Road+East,+Singapore+608567&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=18+Toh+Guan+Road+East,+Singapore+608591&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=26+Kian+Teck+Avenue,+Singapore+628920&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=30+Kian+Teck+Avenue,+Singapore+628921&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=3+Kian+Teck+Lane,+Singapore+627844&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=529+Upper+Jurong+Road,+Singapore+638355&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=2D+Jalan+Papan,+Singapore+619415&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=11+Penjuru+Walk,+Singapore+608541&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=5C+Jalan+Papan,+Singapore+619420&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=58+Penjuru+Place,+Singapore+608562&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=36+Penjuru+Place,+Singapore+608560&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=6+Tuas+South+Street+15,+Singapore+636906&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=80+Tuas+South+Boulevard,+Singapore+637051&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=1+Tuas+South+Street+12,+Singapore+636946&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=70+Tuas+South+Avenue+1,+Singapore+637285&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=69H+Tuas+South+Avenue+1+Singapore+637509&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tuas+South+Boulevard&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=12+Tuas+Drive+1,+Singapore+638679&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=200A+Tuas+South+Boulevard,+Singapore+637060&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=14+Tuas+Ave+1,+Singapore+639499&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Admiralty+Road+West,+Singapore+759956&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=51+Admiralty+Road+West,+Singapore+757443&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=49+Admiralty+Road+West,+Singapore+757444&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=20+Senoko+Drive,+Singapore+758207&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=51+North+Coast+Avenue,+Singapore+756992&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=2+Woodlands+Sector+2,+Singapore+737723&key=xxx
## "2 Woodlands Secto..." not uniquely geocoded, using "2 woodlands sector 2, singapore"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=27+Woodlands+Sector+1,+Singapore+738252&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=50+Woodlands+Industrial+Park+E4,+Singapore+757388&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=190+Woodlands+Industrial+Park+E7,+Singapore+757178&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=460+Mandai+Road,+Singapore+729760&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=23+Mandai+Estate,+Singapore+729937&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=34+Mandai+Estate,+Singapore+729940&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=12+Kranji+Road,+Singapore+739522&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=500+Old+Choa+Chu+Kang+Road,+Singapore+698924&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=8+Seletar+North+Link,+Singapore+797455&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=2+Seletar+North+Link,+Singapore+797601&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=MK31+on+Lot+04962N+at+Changi+Coast+Road&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lamp+post+192,+Changi+Coast+Road&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=80+Tanah+Merah+Coast+Road,+Singapore+498736&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=2+Tampines+Place,+Singapore+528821&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=39+Kaki+Bukit+Avenue+3,+Singapore+415920&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=12+Shaw+Road,+Singapore+367951&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=25+Kaki+Bukit+Road+3,+Singapore+415815&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=60+Boundary+Close,+Singapore+567730&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=33+Harbour+Drive,+Singapore+117606&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=81+Brani+Terminal+Avenue,+Singapore+098323&key=xxx
dormitories %>% view()

Plotting dormitory locations on tmap

dormitories_points <- st_as_sf(dormitories, coords = c("lon", "lat"), crs = 4326) %>%
  st_transform(3414)
dorms_fix_encoding <- dormitories_points %>% 
  mutate(dormitory = iconv(dormitory, to = "UTF-8", sub = ""))
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(dorms_fix_encoding) + tm_dots()

Grocery shops locations

grocery_shops_600m <-  read_csv("data/grocery_shops_locations_test.csv")
## Parsed with column specification:
## cols(
##   Shop = col_character(),
##   Lat = col_double(),
##   Lon = col_double(),
##   Remarks = col_character()
## )

Plot on map

grocery_shops_600m_points <- st_as_sf(grocery_shops_600m, coords = c("Lon", "Lat"), crs = 4326) %>%
  st_transform(3414)
#Little India grocery shops we sampled
grocery_shops_points_little_india <- grocery_shops_600m_points %>%
  filter (Remarks == "Little India")
#Grocery shops near dorms we sampled
grocery_shops_600m_points_dorms <- grocery_shops_600m_points %>%
  filter (Remarks != "Little India")

Supermarkets location (NTUC, ShengSiong, Giant)

supermarket_locations_filtered <- read.csv("data/supermarket_locations_filtered.csv")
supermarket_locations_points <- st_as_sf(supermarket_locations_filtered, coords = c("lon", "lat"), crs = 4326) %>%
  st_transform(3414)
supermarket_locations_points %>%
  tm_shape() + tm_dots()

Visually dorms with supermarkets within 2km

supermarkets_2km <- supermarket_locations_points %>%
  st_buffer(2000)
st_crs(supermarkets_2km)
## Coordinate Reference System:
##   EPSG: 3414 
##   proj4string: "+proj=tmerc +lat_0=1.366666666666667 +lon_0=103.8333333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +units=m +no_defs"
st_crs(supermarkets_2km) <- 3414
supermarkets_2km <- supermarkets_2km %>%
  st_union()
dorms_supermarkets_2km <- dorms_fix_encoding %>% 
  filter(st_intersects(supermarkets_2km, dorms_fix_encoding, sparse = F)) 
#green = those with supermarkets within 2km
tm_shape(dorms_fix_encoding) + tm_dots (col = "red") + tm_shape(dorms_supermarkets_2km) + tm_dots (col = "green")

Supermarkets within 2km of dorm

dorms_2km <- dorms_fix_encoding %>%
  st_buffer(2000)
st_crs(dorms_2km)
## Coordinate Reference System:
##   EPSG: 3414 
##   proj4string: "+proj=tmerc +lat_0=1.366666666666667 +lon_0=103.8333333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +units=m +no_defs"
st_crs(dorms_2km) <- 3414
dorms_2km <- dorms_2km %>%
  st_union()
supermarkets_within_2km_of_dorms <- supermarket_locations_points %>% 
  filter(st_intersects(dorms_2km, supermarket_locations_points, sparse = F)) 
tm_shape(dorms_fix_encoding) + tm_dots (col = "black") + tm_shape(supermarkets_within_2km_of_dorms) + tm_dots (col = "green")

Plotting all grocery shops around dorms

tm_shape(dorms_fix_encoding) + tm_bubbles () + tm_shape(supermarkets_within_2km_of_dorms) + tm_dots (col = "green") + tm_shape(grocery_shops_600m_points_dorms) + tm_dots(col = "blue") + tm_shape(grocery_shops_points_little_india) + tm_dots(col = "purple")

Food outlets locations

food_outlets_600m <- read.csv("data/food_outlets_locations_test.csv")
food_outlets_600m_points <- st_as_sf(food_outlets_600m, coords = c("Lon", "Lat"), crs = 4326) %>%
  st_transform(3414)
#Little India grocery shops we sampled
food_outlets_points_little_india <- food_outlets_600m_points %>%
  filter (Remarks == "Little India")
#Grocery shops near dorms we sampled
food_outlets_600m_points_dorms <- food_outlets_600m_points %>%
  filter (Remarks != "Little India") %>%
  filter (Remarks != "Hawker Centre")

Hawker Centres

hawker_centres_locations <- read.csv("data/hawker_centres_locations_cleaned.csv")
hawker_centres_points <- st_as_sf(hawker_centres_locations, coords = c("lon", "lat"), crs = 4326) %>%
  st_transform(3414)

Visually dorms with hawker centres within 2km

HC_2km <- hawker_centres_points %>%
  st_buffer(2000)
st_crs(HC_2km)
## Coordinate Reference System:
##   EPSG: 3414 
##   proj4string: "+proj=tmerc +lat_0=1.366666666666667 +lon_0=103.8333333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +units=m +no_defs"
st_crs(HC_2km) <- 3414
HC_2km <- HC_2km %>%
  st_union()
dorms_HC_2km <- dorms_fix_encoding %>% 
  filter(st_intersects(HC_2km, dorms_fix_encoding, sparse = F)) 
#green = those with hawker centres within 2km
tm_shape(dorms_fix_encoding) + tm_dots (col = "red") + tm_shape(dorms_HC_2km) + tm_dots (col = "green")

Hawker Centres within 2km of dorm

HC_within_2km_of_dorms <- hawker_centres_points %>% 
  filter(st_intersects(dorms_2km, hawker_centres_points, sparse = F)) 
tm_shape(dorms_fix_encoding) + tm_dots (col = "black") + tm_shape(HC_within_2km_of_dorms) + tm_dots (col = "green")

Plotting all food outlets around dorms

tm_shape(dorms_fix_encoding) + tm_bubbles () + tm_shape(HC_within_2km_of_dorms) + tm_dots (col = "green") + tm_shape(food_outlets_600m_points_dorms) + tm_dots(col = "blue") + tm_shape(food_outlets_points_little_india) + tm_dots(col = "purple")

Bus stop information

bus_stops <-read_sf("data/BusStopLocation_Apr2019/BusStop.shp")
bus_stops %>%
  tm_shape () + tm_dots()

Dormitories with bus stops within 600m

bus_stops_600m <- bus_stops %>%
  st_buffer(600)
st_crs(bus_stops_600m)
## Coordinate Reference System:
##   No EPSG code
##   proj4string: "+proj=tmerc +lat_0=1.366666666666667 +lon_0=103.8333333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +datum=WGS84 +units=m +no_defs"
st_crs(bus_stops_600m) <- 3414
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
bus_stops_600m <- bus_stops_600m %>%
  st_union()
dorms_bus_stops_600m <- dorms_fix_encoding %>% 
  filter(st_intersects(bus_stops_600m, dorms_fix_encoding, sparse = F)) 
#green = those with bus stops 
tm_shape(dorms_fix_encoding) + tm_dots (col = "red") + tm_shape(dorms_bus_stops_600m) + tm_dots (col = "green")

Bus Stops within 600m of dorms

dorms_600m <- dorms_fix_encoding %>%
  st_buffer(600)
st_crs(dorms_600m)
## Coordinate Reference System:
##   EPSG: 3414 
##   proj4string: "+proj=tmerc +lat_0=1.366666666666667 +lon_0=103.8333333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +units=m +no_defs"
st_crs(dorms_600m) <- 3414
st_crs(bus_stops) <- 3414
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
dorms_600m <- dorms_600m %>%
  st_union()
bus_stops_within_600m_of_dorms <- bus_stops %>% 
  filter(st_intersects(dorms_600m, bus_stops, sparse = F)) 
#green = those with bus stops 
tm_shape(dorms_fix_encoding) + tm_bubbles () + tm_shape(bus_stops_within_600m_of_dorms) + tm_dots (col = "blue")